package com.linciping.useges.plugin.view;

import com.intellij.util.ui.tree.AbstractTreeModel;
import com.linciping.useges.plugin.bean.UsegesNode;
import com.linciping.useges.plugin.util.CollectionUtil;

import javax.swing.tree.TreePath;

public class UsegesModel extends AbstractTreeModel {

    private final UsegesNode rootNode;

    public UsegesModel(UsegesNode rootNode) {
        this.rootNode = rootNode;
    }

    @Override
    public Object getRoot() {
        return rootNode;
    }

    @Override
    public Object getChild(Object parent, int index) {
        UsegesNode parentNode = (UsegesNode) parent;
        return parentNode.getChildrenList().get(index);
    }

    @Override
    public int getChildCount(Object parent) {
        UsegesNode parentNode = (UsegesNode) parent;
        return parentNode.getChildrenList().size();
    }

    @Override
    public boolean isLeaf(Object node) {
        UsegesNode usegesNode = (UsegesNode) node;
        return CollectionUtil.isListEmpty(usegesNode.getChildrenList());
    }

    @Override
    public void valueForPathChanged(TreePath path, Object newValue) {

    }

    @Override
    public int getIndexOfChild(Object parent, Object child) {
        UsegesNode parentNode = (UsegesNode) parent;
        UsegesNode childNode = (UsegesNode) child;
        return parentNode.getChildrenList().indexOf(childNode);
    }
}
